
<?php

/**添加书籍
 * 
 */


$dblink = new DataBase();
$query = "select * from cate order by sort ASC";
$cates = $dblink->query($query)->fetchall();

if($_POST) {
    $file_name = md5(time());
    if(in_array('', $_POST)) {
        Common::Msg('请检查内容是否填写完整!');
        
    }
    $epub_ext = substr($_FILES['epub']['name'], -4, 4);
    $epub_size = number_format($_FILES['epub']['size'] / 1024, 1);
    
    if($epub_ext !='epub') {
        Common::Msg('书籍文件格式错误,请返回检查!');
    }
    
    
    $dir_name = date('Ymd');
    if(!file_exists('books/'.$dir_name)) {
        mkdir('books/'.$dir_name);
    }
    if(!file_exists('images/src/'.$dir_name)) {
        mkdir('images/src/'.$dir_name);
    }
    if(!file_exists('images/small/'.$dir_name)) {
        mkdir('images/small/'.$dir_name);
    }
    if(!file_exists('images/big/'.$dir_name)) {
        mkdir('images/big/'.$dir_name);
    }
    
    @move_uploaded_file($_FILES['epub']['tmp_name'], 'books/'.$dir_name.'/'.$file_name.'.epub');
    
    if(empty($_FILES['pic']['tmp_name'])) {
        Common::Msg('图片上传失败!');
    }

    $pic_ext = explode('.', $_FILES['pic']['name']);
    $pic_ext = $pic_ext[1];
    
    $src_patch = 'images/src/'.$dir_name.'/'.$file_name.'.'.$pic_ext;
    $big_patch = 'images/big/'.$dir_name.'/'.$file_name.'.'.$pic_ext;
    $small_patch = 'images/small/'.$dir_name.'/'.$file_name.'.'.$pic_ext;
    
    if($pic_ext != 'png' && $pic_ext != 'jpg' && $pic_ext != 'jpeg') {
        Common::Msg('图片格式不正确');
    }
    
    if(!move_uploaded_file($_FILES['pic']['tmp_name'], $src_patch)) {
        Common::Msg('图片上传失败!');
    }
    
    require_once(ROOT.'/class/simpleimage.class.php');
    $simple_image = new SimpleImage();
    
    $simple_image->load($src_patch);
    $simple_image->resizeToWidth(160);
    $simple_image->save($big_patch);
    
    $simple_image->load($src_patch);
    $simple_image->resizeToWidth(54);
    $simple_image->save($small_patch);

    Common::imageWaterMark($big_patch, 8, 'theme/watermark.png');
    
    $cate_id = $_POST['cate'];
    $title = $_POST['title'];
    $author = $_POST['author'];
    $content = $_POST['content'];
    $file = $file_name.'.epub';
    $pic = $file_name.'.'.$pic_ext;
    $size = $epub_size;
    $title_md5 = md5($title);
    $author_md5 = md5($author);
    $created_at = date('Y-m-d H:i:s');    
    
    $dblink = new DataBase();
    
    $query = "insert into books set cateid = '$cate_id', title = '$title', author = '$author', content = '$content', file = '$file', pic = '$pic', size = '$size', title_md5 = '$title_md5', author_md5 = '$author_md5' , created_at = '$created_at'";
    
    $dblink->query($query);
    
    $query = "select * from cate where id = '$cate_id'";
    $cate_info = $dblink->query($query)->fetch();

    $query = "update cate set count = '" . strval($cate_info['count'] + 1) . "' where id = '$cate_id'";
    $dblink->query($query);
    
    Common::Msg('添加成功!');
    
}
    
?>

<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="Zend Studio" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" href="theme/style.css" rel="stylesheet"/> 
<script type="text/javascript" src="theme/jquery.js"></script>
<script type="text/javascript">
    function clear_textarea(obj) {
        if(obj.value == '请在此输入书籍简介.') {
            obj.value = '';
        }
    }
</script>
<script type="text/javascript">
    function check_form(obj) {
        
       if(obj.title.value == '') {
           alert('请填写书名!');
           obj.title.focus();  
           return false;  
       }
       
       if(obj.author.value == '') {
           alert('请填写作者!');
           obj.author.focus();  
           return false;  
       }
       if(obj.cate.value == 0) {
           alert('请选择书籍分类!');
           obj.cate.focus();  
           return false;  
       }
       if(obj.content.value == '请在此输入书籍简介.' || obj.content.value == '') {
           alert('请填写书籍简介!');
           obj.content.focus();  
           return false;  
       }
       
       if(obj.epub.value == '') {
           alert('请选择书籍文件!');
           obj.epub.focus();  
           return false;  
       }
       
       if(obj.pic.value == '') {
           alert('请选择书籍图片!');
           obj.pic.focus();  
           return false;  
       }
       
    }
    
</script>
<title>stanza 书苑</title>
</head>
<body>
    <div class="main">
        <div class="content">
            <div class="left">
            <h1>添加书籍:</h1>
            <div class="line"></div>
            <form action="index.php?action=add" method="post" enctype="multipart/form-data" onsubmit="javascript:return check_form(this);">   
                <div class="list">
                    <p>书　　名: <input type="text" name="title" class="input" /></p>
                    <p>作　　者: <input type="text" name="author" class="input" /></p>
                    <p>分　　类: <select name="cate" class="select">
                                    <option value="0">--请选择--</option>
                                    <?php foreach ($cates as $cate): ?>
                                    <option value="<?php echo $cate['id'] ?>"><?php echo $cate['title'] ?></option>
                                    <?php endforeach; ?>
                                </select>
                    </p>
                    <p>简　　介:</p>
                    <p><textarea name="content" rows="10" onclick="javascritp:clear_textarea(this);"/>请在此输入书籍简介.</textarea></p>
                    <p>书籍文件: <input type="file" name="epub" class="file" size="45" /></p>
                    <p>书籍图片: <input type="file" name="pic" class="file" size="45" /></p>
                    <p><input type="submit" value="确认无误, 我要提交." class="button"/></p>
                    <div class="line"></div>
                </div>
            </form>
            </div>
        </div>
    </div>
</body>
</html>

